//
//  DestinationController.m
//  TravelNotes
//
//  Created by qianfeng on 15-7-7.
//  Copyright (c) 2015年 ZWB. All rights reserved.
//

#import "DestinationController.h"
#import "AFNetworking.h"
#import "DestinationCell.h"

#define kPadding 10

@interface DestinationController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
{
    AFHTTPRequestOperationManager *_manager;
}

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *titleArray;

@end

@implementation DestinationController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.collectionView.backgroundColor=[UIColor whiteColor];
    [self initUI];
    [self firstDownload];
}

- (void)initUI {
    self.titleArray = [NSMutableArray arrayWithObjects:@"国外·亚洲", @"国外·欧洲", @"美洲、大洋洲、非洲与南极洲", @"国内大陆", nil];
    self.dataArray = [[NSMutableArray alloc] init];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    NSLog(@"%lf", (kScreenSize.width - kPadding*2)/2);
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.minimumLineSpacing = 5;
    flowLayout.itemSize = CGSizeMake((kScreenSize.width - kPadding * 3)/2, 220);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height-100) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"DestinationCell" bundle:nil] forCellWithReuseIdentifier:@"DestinationCell"];

    _manager = [AFHTTPRequestOperationManager manager];
    _manager.responseSerializer = [AFHTTPResponseSerializer serializer];
}

- (void)firstDownload {
    NSString *url = [NSString stringWithFormat:kDestinationUrl];
    [self addTaskWithUrl:url isRefresh:NO];
}

- (void)addTaskWithUrl:(NSString *)url isRefresh:(BOOL)isRefresh {
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            for (NSDictionary *dict in array) {
                DestinationModel *model = [[DestinationModel alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                
                NSMutableArray *destinationArray = [NSMutableArray array];
                for (NSDictionary *dict2 in dict[@"destinations"]) {
                    DestinationsModel *desModel = [[DestinationsModel alloc] init];
                    [desModel setValuesForKeysWithDictionary:dict2];
                    [destinationArray addObject:desModel];
                }
                model.destinations = destinationArray;
            
                [self.dataArray addObject:model];
            }
            [self.collectionView reloadData];
        }
        NSLog(@"下载成功");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载失败");
    }];
}

#pragma mark - delegate 
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return self.dataArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    DestinationModel *model = self.dataArray[section];
    NSArray *array = model.destinations;
//    NSArray *arr  = [self.dataArray[section] destinations];
    
    return array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    DestinationCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DestinationCell" forIndexPath:indexPath];
    DestinationModel *model = self.dataArray[indexPath.section];
    DestinationsModel *desModel = model.destinations[indexPath.row];
    [cell showDataWithModel:desModel];
    
    return cell;
}

@end
